using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace url2jpeg
{
    public partial class capture : LocalizedUserControl
    {
        url2jpegCtn.navigator m_navigator;
        Bitmap m_bitmap;
        pixtek.PlaceObject m_po;
        bool m_poVisible = false;

        public delegate void GenScriptHandler();
        public event GenScriptHandler GenScriptEvent;

        public capture()
        {
            InitializeComponent();
            m_po = new pixtek.PlaceObject();
            m_po.FullDrag = true;
            m_po.SetCursorEvent += new pixtek.PlaceObject.SetCursorHandler(m_po_SetCursorEvent);
            m_po.DragPositionEvent += new pixtek.PlaceObject.DragPositionHandler(m_po_PositionEvent);
            m_po.FinalPositionEvent += new pixtek.PlaceObject.FinalPositionHandler(m_po_PositionEvent);
        }

        void m_po_PositionEvent(pixtek.PlaceObject sender)
        {
            autoScroll1.DirectPaint();
        }

        void m_po_SetCursorEvent(pixtek.PlaceObject sender, Cursor cursor, int areaIndex)
        {
            Cursor = cursor;
        }

        public void SetNavigator(url2jpegCtn.navigator nav)
        {
            m_navigator = nav;
        }

        public void RefreshDisplay()
        {
            if (m_bitmap != null)
                m_bitmap.Dispose();
            m_bitmap = m_navigator.CaptureBitmap();
            m_navigator.FillTreeView(treeView1);
            autoScroll1.SetDocSize(m_bitmap.Width, m_bitmap.Height);
        }

        private void btnFull_CheckedChanged(object sender, EventArgs e)
        {
            m_poVisible = false;
            autoScroll1.DirectPaint();
        }

        private void btnManual_CheckedChanged(object sender, EventArgs e)
        {
            if (m_bitmap != null) // Added version 1.0 silent build 1
            {
                m_po.InitialRectangle = new Rectangle(20, 20, 50, 50);
                m_po.ClippingRectangle = new Rectangle(0, 0, m_bitmap.Width, m_bitmap.Height);
                m_po.Selected = true;
                m_poVisible = true;
                autoScroll1.DirectPaint();
            }
        }

        private void autoScroll1_DoPaintingEvent(Graphics dc, Rectangle clientRectangle, int level)
        {
            if (m_bitmap != null)
            {
                dc.DrawImage(m_bitmap, 0, 0);
                if (m_poVisible)
                    m_po.Paint(dc, Pens.Red, Pens.Red, Pens.Black, Brushes.White);
            }
        }

        private void autoScroll1_SizeChanged(object sender, EventArgs e)
        {
            if (m_bitmap!=null)
                autoScroll1.SetDocSize(m_bitmap.Width, m_bitmap.Height);
        }

        private void autoScroll1_DrawBackgroundEvent(Graphics dc, Rectangle DocArea, Rectangle ViewSize)
        {
            dc.FillRectangle(Brushes.LightGray, 0, 0, ViewSize.Width, ViewSize.Height);
        }

        private int GetTxtInt(TextBox txt, int def)
        {
            int result = def;
            string final=string.Empty;
            for (int i = 0; i < txt.Text.Length; i++)
            {
                char c = txt.Text[i];
                if (c >= '0' && c <= '9')
                    final += c;
            }
            if (final.Length > 0)
                result = Convert.ToInt32(final);

            return result;
        }

        private Bitmap GetBitmap()
        {
            Bitmap finalBmp;
            if (m_poVisible || btnFixedSize.Checked || btnPercentSize.Checked)
            {
                Rectangle srcRect;
                if (m_poVisible)
                    srcRect = m_po.CurrentRectangle;
                else
                    srcRect = new Rectangle(0, 0, m_bitmap.Width, m_bitmap.Height);

                if (btnFixedSize.Checked)
                    finalBmp = new Bitmap(GetTxtInt(txtFixedWidth, m_bitmap.Width), GetTxtInt(txtFixedWidth, m_bitmap.Height));
                else if (btnPercentSize.Checked)
                    finalBmp = new Bitmap(Convert.ToInt32(srcRect.Width * GetTxtInt(txtPercentHeight, 100) / 100),
                        Convert.ToInt32(srcRect.Height * GetTxtInt(txtPercentHeight, 100) / 100));
                else
                    finalBmp = new Bitmap(m_po.CurrentRectangle.Width, m_po.CurrentRectangle.Height);
                Graphics gr = Graphics.FromImage(finalBmp);
                Rectangle destRect = new Rectangle(0, 0, finalBmp.Width, finalBmp.Height);
                gr.DrawImage(m_bitmap, destRect, srcRect, GraphicsUnit.Pixel);
                gr.Dispose();
            }
            else
                finalBmp = new Bitmap(m_bitmap);

            settings.LicenceChecking(finalBmp);

            return finalBmp;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (m_bitmap != null)
            {
                if (saveBitmap.ShowDialog() == DialogResult.OK)
                {
                    Bitmap finalBmp = GetBitmap();

                    System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Jpeg;
                    string ext = System.IO.Path.GetExtension(saveBitmap.FileName);
                    switch (ext.ToLower())
                    {
                        case ".bmp":
                            format = System.Drawing.Imaging.ImageFormat.Bmp;
                            break;
                        case ".png":
                            format = System.Drawing.Imaging.ImageFormat.Png;
                            break;
                    }
                    finalBmp.Save(saveBitmap.FileName, format);
                    if (btnPostview.Checked)
                        System.Diagnostics.Process.Start(saveBitmap.FileName);
                    finalBmp.Dispose();
                }
            }
        }

        private void btnClipboard_Click(object sender, EventArgs e)
        {
            if (m_bitmap != null)
            {
                Bitmap finalBmp = GetBitmap();
                Clipboard.SetData(DataFormats.Bitmap, finalBmp);
                if (finalBmp != m_bitmap)
                    finalBmp.Dispose();
                lblDone.Visible = true;
                timer1.Enabled = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            lblDone.Visible = false;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            url2jpegCtn.navigator.Element element = e.Node as url2jpegCtn.navigator.Element;
            if (element != null)
            {
                m_po.InitialRectangle = element.m_position;
                autoScroll1.SetTLDocPosition(m_po.InitialRectangle.Left, m_po.InitialRectangle.Top);
                autoScroll1.DirectPaint();
            }
        }

        private void autoScroll1_MouseDown(object sender, MouseEventArgs e)
        {
            Point p = autoScroll1.ViewToDoc(new Point(e.X, e.Y));
            m_po.MouseDown(p.X, p.Y);
        }

        private void autoScroll1_MouseMove(object sender, MouseEventArgs e)
        {
            Point p = autoScroll1.ViewToDoc(new Point(e.X, e.Y));
            m_po.MouseMove(p.X, p.Y);
        }

        private void autoScroll1_MouseUp(object sender, MouseEventArgs e)
        {
            Point p = autoScroll1.ViewToDoc(new Point(e.X, e.Y));
            m_po.MouseUp(p.X, p.Y);
        }

        private void btnScript_Click(object sender, EventArgs e)
        {
            if (GenScriptEvent != null)
                GenScriptEvent();
        }

        public void FillForScript(scriptData data)
        {
            if (btnFull.Checked)
            {
                data.capture = captureMode.Full;
            }
            else
            {
                data.capture = captureMode.Manual;
                data.captureParam = m_po.CurrentRectangle;
            }
        }
    }
}
